<template>
	<view class="team">
		<view class="order_type_tab_box">
			<view
				:class="['order_type_tab_head', item.status == pays.status ? 'order_type_tab_head_active' : '']"
				@click="handleChangeStatus(item)"
				v-for="item in tabStatus"
				:key="item.status">
				<view class="type_text">{{ item.name }}</view>
				<view class="active_type"></view>
			</view>
		</view>
		<view class="header_box">
			<view class="content_search">
				<view class="content_search_left">
					<view class="">
						<!-- <image src="/static/images/circles/quanzi_sousuo_black.png" mode=""></image> -->
						<input maxlength="10" type="text" @input="inpFocus" v-model="pays.keyWord" placeholder="输入用户昵称/手机号/ID" placeholder-style="color:#CACCCC" />
					</view>
					<!-- <image v-if="pays.close" @tap="closeBtn" class="search_close" src="../../../../static/logo.png"></image> -->
					<uni-icons v-if="pays.close" @tap="closeBtn" type="close" size="24"></uni-icons>
				</view>
				<view class="content_search_right" @tap="searchBtn">搜索</view>
			</view>
		</view>
		<view class="list">
			<template v-if="pays.status == 1">
				<view class="item" @click="toDetail(item)" v-for="(item, index) in pays.list" :key="index">
					<view class="referee">
						推荐人：
						<image :src="item.refereeAvatar" mode=""></image>
						<text class="option">{{ item.refereeRealName }}</text>
						<text>{{ item.refereeId }}</text>
					</view>
					<view class="info">
						<view class="userInfo">
							<image :src="item.avatar" mode=""></image>
							<view class="detailWrap">
								<view class="identity">
									<view class="name">
										<view class="realName">{{ item.realName }}</view>
										<view class="nickName">{{ item.nickName }}</view>
									</view>
									<view class="identityDetail" v-if="item.identity == 1">店长</view>
								</view>
								<view class="userId">ID:{{ item.id }}</view>
								<view class="age">
									{{ item.sex == 0 ? "女" : "男" }} | {{ item.age }}岁 | {{ item.phone }}
									<text class="copy" @click.prevent="copy(item)">复制</text>
								</view>
							</view>
						</view>
						<!-- <view class="btn" @click.prevent="contact(item)">联系TA</view> -->
					</view>
					<view class="service">
						<view class="serviceItem_top">
							<view class="serviceLabel">线上商城</view>
							<view class="serviceVal xjs-ml-16">推广收入占比{{ handleIncome(item.mallPromotionRatio) }}%</view>
						</view>
						<view class="serviceItem_bottom">
							<view class="serviceLabel">服务订单</view>
							<view class="serviceVal">
								<text class="xjs-ml-16 xjs-mr-16">推广收入占{{ handleIncome(item?.servicePromotionRatio) }}%</text>
								接单收入占比为{{ handleIncome(item?.serviceAcceptanceRatio) }}%
							</view>
						</view>
					</view>
					<view class="num">
						<view class="numItem">
							<view class="numLabel">已结算收入</view>
							<view class="numVal">{{ item.balance || 0 }}</view>
						</view>
						<view class="numItem">
							<view class="numLabel">待提现余额</view>
							<view class="numVal">{{ item.orderAmount || 0 }}</view>
						</view>
						<view class="numItem">
							<view class="numLabel">总订单数</view>
							<view class="numVal">{{ item.totalOrderNum || 0 }}</view>
						</view>
						<view class="numItem">
							<view class="numLabel">消费金额</view>
							<view class="numVal">{{ item.totalAmount || 0 }}</view>
						</view>
					</view>
				</view>
			</template>
			<template v-if="pays.status == 2">
				<view class="item" @click="toDetail(item)" v-for="(item, index) in pays.list" :key="index">
					<view class="referee">
						推荐人：
						<image :src="item.refereeAvatar" mode=""></image>
						<text class="option">{{ item.refereeRealName }}</text>
						<text>{{ item.refereeId }}</text>
					</view>
					<view class="info">
						<view class="userInfo">
							<image :src="item.avatar" mode=""></image>
							<view class="detailWrap">
								<view class="identity">
									<view class="name">
										<view class="realName">{{ item.realName }}</view>
										<view class="nickName">{{ item.nickName }}</view>
									</view>
									<view class="identityDetail" v-if="item.identity == 1">店长</view>
								</view>
								<view class="userId">ID:{{ item.id }}</view>
								<view class="age">
									{{ item.sex == 0 ? "女" : "男" }} | {{ item.age }}岁 | {{ item.phone }}
									<text class="copy" @click.prevent="copy(item)">复制</text>
								</view>
							</view>
						</view>
						<!-- <view class="btn" @click.prevent="contact(item)">联系TA</view> -->
					</view>
					<view class="num">
						<view class="numItem">
							<view class="numLabel">本工作室订单数：</view>
							<view class="numVal">{{ item.studioOrderNum || 0 }}</view>
						</view>
						<view class="numItem">
							<view class="numLabel">总订单数：</view>
							<view class="numVal">{{ item.countOrderNum || 0 }}</view>
						</view>
					</view>
				</view>
			</template>
			<uni-load-more iconType="auto" :status="pays.loading" />
		</view>
	</view>
</template>

<script setup lang="ts">
import { onLoad, onShow } from "@dcloudio/uni-app";
import { reactive, onMounted, ref } from "vue";
import { onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";
import api from "@/api";
import BigNumber from "bignumber.js";

const tabStatus = reactive([
	{ name: "陪宠师", status: 1 },
	{ name: "用户", status: 2 }
]);
const pays = reactive<{
	query: Partial<{
		select: string | boolean;
	}>;
	list: [];
	loading: string;
	page: number;
	limit: number;
	studio: any;
	status: number | string;
	keyWord: string;
	close: boolean;
	isSearch: boolean;
}>({
	query: {},
	list: [],
	loading: "more",
	page: 1,
	limit: 20,
	studio: {},
	status: 1,
	keyWord: "",
	close: false,
	isSearch: false
});

onShow(async () => {
	await getStudioInfo();
	if (pays.status == 1) {
		await getList();
	} else {
		await getUserList();
	}
	scrollToTop();
});

const filterPetService = (type) => {
	if (type == 1) {
		return "上门喂养";
	} else if (type == 2) {
		return "上门遛狗";
	} else if (type == 3) {
		return "基础清洁";
	}
};
const handleIncome = (proportion: any) => {
	if (proportion) {
		return new BigNumber(100).times(proportion).toNumber();
	} else {
		return 0;
	}
};
const copy = (item) => {
	uni.setClipboardData({
		data: item.phone,
		success: function () {
			uni.showToast({
				title: "复制成功",
				icon: "none"
			});
		}
	});
};

const filterServiceType = (item) => {
	return item.serviceType.split(",");
};

const contact = (item) => {
	uni.makePhoneCall({
		phoneNumber: item.phone
	});
};

const toDetail = (item) => {
	uni.navigateTo({
		url: `/pages/myStudio/studio/teamDetails/index?petsitterId=${item.petsitterId}&studioId=${pays.studio.id}&userId=${item.id}&status=${pays.status}`
	});
};

const getStudioInfo = async () => {
	const res = await api({
		method: "get",
		url: `/mall/studio/myStudio`
	});
	pays.studio = res.data.studio;
};

// 输入框实时检测
const inpFocus = () => {
	if (pays.keyWord.trim().length > 0) {
		pays.close = true;
	} else {
		pays.close = false;
	}
};

const closeBtn = async () => {
	pays.keyWord = "";
	pays.close = false;
	if (pays.isSearch) {
		await onDataInit();
		if (pays.status == 1) {
			await getList();
		} else {
			await getUserList();
		}
		pays.keyWord = "";
		pays.isSearch = false;
	}
};

const searchBtn = async () => {
	if (pays.keyWord == "") {
		uni.showToast({
			title: "请输入关键词后再搜索",
			icon: "none"
		});
	} else {
		await onDataInit();
		pays.isSearch = true;
		if (pays.status == 1) {
			await getList();
		} else {
			await getUserList();
		}
	}
};

const handleChangeStatus = async (item: any) => {
	pays.status = item.status;
	pays.list = [];
	pays.keyWord = "";
	pays.close = false;
	await onDataInit();
	if (pays.status == 1) {
		await getList();
	} else {
		await getUserList();
	}
	scrollToTop();
};

const getUserList = async () => {
	uni.showLoading({
		title: "加载中",
		mask: true
	});
	pays.loading = "loading";
	const {
		data: { total = 0, records = [] }
	}: any = await api({
		method: "get",
		url: `/mall/studio/petsitter-team/getTeamUserPage`,
		data: {
			page: pays.page,
			limit: pays.limit,
			studioId: pays.studio.id,
			keyWord: pays.keyWord
		}
	});
	if (records) {
		pays.list = pays.page == 1 ? records : pays.list.concat(records);
		if (!records.length || pays.list.length >= total) {
			pays.loading = "noMore";
		} else {
			pays.loading = "more";
		}
		console.log(pays.list, "pays.listuser");
	} else {
		pays.loading = "more";
	}
	uni.hideLoading();
};

const getList = async () => {
	uni.showLoading({
		title: "加载中",
		mask: true
	});
	pays.loading = "loading";
	const {
		data: { total = 0, records = [] }
	}: any = await api({
		method: "get",
		url: `/mall/studio/petsitter-team/getTeamPage`,
		data: {
			page: pays.page,
			limit: pays.limit,
			studioId: pays.studio.id,
			keyWord: pays.keyWord
		}
	});
	if (records) {
		pays.list = pays.page == 1 ? records : pays.list.concat(records);
		if (!records.length || pays.list.length >= total) {
			pays.loading = "noMore";
		} else {
			pays.loading = "more";
		}
		console.log(pays.list, "pays.list");
	} else {
		pays.loading = "more";
	}
	uni.hideLoading();
};

const onDataInit = async () => {
	pays.page = 1;
};
const scrollToTop = () => {
	uni.pageScrollTo({
		scrollTop: 0,
		duration: 300
	});
};
// 页面下拉刷新
onPullDownRefresh(async () => {
	console.log("onPullDownRefresh");
	if (pays.loading == "loading") {
		uni.stopPullDownRefresh();
	} else {
		try {
			await onDataInit();
			if (pays.status == 1) {
				await getList();
			} else {
				await getUserList();
			}
		} catch (error) {}
		uni.stopPullDownRefresh();
	}
});
// 触底
onReachBottom(async () => {
	console.log("onReachBottom");
	if (pays.loading == "noMore") return false;
	pays.page++;
	if (pays.status == 1) {
		await getList();
	} else {
		await getUserList();
	}
});
</script>

<style lang="scss" scoped>
.team {
	width: 100%;
	height: 100%;
	background-color: #f9f6f6;
	// padding: 0 32rpx 0;
	box-sizing: border-box;

	.hidden {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.order_type_tab_head_active {
		.type_text {
			font-size: 32rpx !important;
			color: #1b2120 !important;
			font-weight: bold !important;
		}

		.active_type {
			bottom: 0;
			width: 32rpx;
			height: 8rpx;
			background: var(--color-primary);
			border-radius: 4rpx;
		}
	}

	.order_type_tab_box {
		// position: sticky;
		// top: 0;
		display: flex;
		width: 100%;
		background: #ffffff;
		padding: 0 40rpx;
		box-sizing: border-box;
		border-top: 1rpx solid #e4e6e6;
		z-index: 1;

		.order_type_tab_head {
			flex: 1;
			position: relative;
			display: flex;
			flex-direction: column;
			align-items: center;
			height: 88rpx;
			padding-top: 20rpx;
			box-sizing: border-box;

			.type_text {
				font-weight: 400;
				font-size: 28rpx;
				height: 100%;
				color: #5c5e5e;
				text-align: center;
			}
		}
	}

	.header_box {
		padding: 0 32rpx;
		box-sizing: border-box;
		background-color: #fff;
		position: sticky;
		z-index: 10;
		top: 0;
		/* #ifdef H5 */
		top: 86rpx;
		/* #endif */

		.content_search {
			padding: 8rpx 0;
			display: flex;
			align-items: center;
			justify-content: space-between;
			line-height: 1;

			.content_search_left {
				padding: 12rpx 24rpx;
				width: 604rpx;
				height: 72rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				background-color: #f5f7f6;
				border-radius: 200rpx;
				box-sizing: border-box;

				view {
					width: 90%;
					display: flex;
					align-items: center;

					image {
						margin-right: 14rpx;
						width: 36rpx;
						height: 36rpx;
					}

					input {
						width: 100%;
						font-size: 28rpx;
					}
				}

				.search_close {
					display: block;
					width: 36rpx;
					height: 36rpx;
				}
			}

			.content_search_right {
				font-size: 32rpx;
				color: #ff6084;
				font-weight: bold;
			}
		}
	}

	.list {
		padding: 32rpx 32rpx 0;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 64rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 64rpx);

		.item {
			width: 100%;
			padding: 40rpx 32rpx;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 24rpx 24rpx 24rpx 24rpx;
			margin-bottom: 24rpx;

			.referee {
				display: flex;
				align-items: center;
				color: #5c5e5e;
				font-size: 26rpx;
				margin-bottom: 24rpx;

				image {
					width: 44rpx;
					height: 44rpx;
					border-radius: 50%;
					margin-right: 10rpx;
				}

				.option {
					position: relative;
					margin-right: 20rpx;

					&:last-child {
						margin-right: 0;

						&::after {
							content: none;
						}
					}

					&::after {
						content: "";
						position: absolute;
						right: -10rpx;
						transform: translateY(-50%);
						top: 50%;
						height: 26rpx;
						width: 1rpx;
						background-color: #5c5e5e;
					}
				}
			}

			.info {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 32rpx;

				.userInfo {
					display: flex;
					justify-content: space-between;
					align-items: center;

					image {
						width: 64rpx;
						height: 64rpx;
						border-radius: 50%;
					}

					.userId {
						font-size: 26rpx;
						color: #1b2120;
						margin: 10rpx 0;
					}

					.detailWrap {
						margin-left: 16rpx;

						.identity {
							display: flex;
							align-items: center;

							.name {
								font-weight: bold;
								font-size: 32rpx;
								color: #1b2120;
								display: flex;
								align-items: flex-end;

								.realName {
									margin-right: 14rpx;
								}

								.nickName {
									color: #5c5e5e;
									font-size: 24rpx;
									// margin-left: 14rpx;
									// max-width: 180rpx;
									// overflow: hidden;
									// text-overflow: ellipsis;
									// white-space: nowrap
								}
							}

							.identityDetail {
								font-size: 24rpx;
								color: #fff;
								background: linear-gradient(90deg, #ffa6bc 0%, #ffa3a3 100%);
								border-radius: 40rpx 40rpx 40rpx 40rpx;
								width: 64rpx;
								line-height: 32rpx;
								text-align: center;
								margin-left: 8rpx;
							}
						}

						.age {
							font-size: 26rpx;
							color: #7c807f;
							margin-top: 8rpx;

							.copy {
								color: #ff6084;
								text-decoration: underline;
							}
						}
					}
				}

				.btn {
					font-size: 26rpx;
					color: #fff;
					text-align: center;
					width: 160rpx;
					line-height: 72rpx;
					background: linear-gradient(90deg, #ff6188 0%, #ff5252 100%);
					border-radius: 62rpx 62rpx 62rpx 62rpx;
				}
			}

			.service {
				box-sizing: border-box;
				border-radius: 24rpx;

				.serviceItem_bottom {
					width: 622rpx;
					height: 74rpx;
					line-height: 74rpx;
					display: flex;
					background: #f1eeff;
					border-radius: 16rpx;
					// align-items: center;
					font-size: 26rpx;
					margin-bottom: 32rpx;

					&:last-child {
						margin-bottom: 0;
					}

					.serviceLabel {
						margin-left: 24rpx;
						color: #7c807f;
						font-weight: bold;
					}

					.serviceVal {
						color: #5c5e5e;
					}
				}

				.serviceItem_top {
					width: 622rpx;
					height: 74rpx;
					line-height: 74rpx;
					background: #ffe6ec;
					border-radius: 16rpx;
					display: flex;
					// align-items: center;
					font-size: 26rpx;
					margin-bottom: 16rpx;

					&:last-child {
						margin-bottom: 0;
					}

					.serviceLabel {
						color: #7c807f;
						font-weight: bold;
						margin-left: 24rpx;
					}

					.serviceVal {
						color: #5c5e5e;
					}
				}

				.serviceOption {
					position: relative;
					margin-right: 16rpx;

					&:last-child {
						margin-right: 0;

						&::after {
							content: none;
						}
					}

					&::after {
						content: "";
						position: absolute;
						right: -8rpx;
						transform: translateY(-50%);
						top: 50%;
						height: 24rpx;
						width: 1rpx;
						background-color: #cacccc;
					}
				}
			}

			.num {
				display: flex;
				justify-content: space-around;
				padding: 32rpx 0;
				align-items: center;
				margin-top: 16rpx;
				background: #f9f6f6;
				border-radius: 16rpx;

				.numItem {
					font-size: 26rpx;
					box-sizing: border-box;

					.numLabel {
						font-size: 24rpx;
						color: #7c807f;
						margin-bottom: 16rpx;
					}

					.numVal {
						color: #5c5e5e;
						font-weight: bold;
					}
				}
			}
		}
	}
}
</style>
